package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBSalgsVare implements IFDBSalgsVare {
	private Connection con;
	private PreparedStatement pStmtSelect;
	
	public DBSalgsVare()
	{
		con = DBConnection.getInstance().getDBcon();
		pStmtSelect = null;
	}
	
	public SalgsVare findWithPreQuery(int svId, boolean retriveAssociation)
    {   System.out.println("Find with preCompiled");
        if(pStmtSelect == null){
           pStmtSelect =  getPreparedStmt();          
        }
        SalgsVare svObj = null;
        ResultSet results;
        try{
       
        pStmtSelect.setInt(1,svId);
        results = pStmtSelect.executeQuery();
        if(results.next()){
             svObj = buildSalgsVare(results);	
        }
        }
        catch(Exception e){
            e.getMessage();
        }
        return svObj;
    }
	
	private PreparedStatement getPreparedStmt()
    {
       try{ 
        String query = "select * from SalgsVare" +
                       " where salgsVareId = ?";
        System.out.println("Preparestatement " + query);
        pStmtSelect = con.prepareStatement(query);
        
       }
       catch(Exception e){
            e.getMessage();
        }
       return pStmtSelect;
    }

	
	private ArrayList<SalgsVare> miscWhere(String wClause, boolean retrieveAssociation)
	{
        ResultSet results;
	    ArrayList<SalgsVare> list = new ArrayList<SalgsVare>();	
		
	    String query =  buildQuery(wClause);
	    System.out.println("print noget ud " +query+"");
            try{ // read the produkt from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	SalgsVare salgsVObj = new SalgsVare();
	     	salgsVObj = buildSalgsVare(results);	
                 list.add(salgsVObj);	
		}//end while
                 stmt.close();     
                 if(retrieveAssociation)
                 {   //The supervisor and department is to be build as well
                     for(SalgsVare salgsVObj : list){
                    	 DBOrdre ordre = new DBOrdre();
                    	 Ordre ordre2 = ordre.findOrdreId(salgsVObj.getSalgsVare().getOrderId(), false);
                    	 
                    	 
                    	 salgsVObj.setSalgsVare(ordre2);
                    	 
                    	 
                    	 DBProdukt produkt = new DBProdukt();
                    	 Produkt produkt2 = produkt.findProduktId(salgsVObj.getProduktId().getProduktId(), false);
                    	 salgsVObj.setProduktId(produkt2);
                    	 
                    	
                       // here the department has to be selected as well
                     }
      
                 }//end if   
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	

	
	private SalgsVare singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		SalgsVare svObj = new SalgsVare();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the salgsvare from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
	 			svObj = buildSalgsVare(results);
                            //assocaition is to be build
                            stmt.close();
                            
			}
                        else{ //no employee was found
                        	svObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return svObj;
	}
	
	
	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM SalgsVare";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	//method to build an SaleLineItem object
	private SalgsVare buildSalgsVare(ResultSet results)
      {   
		SalgsVare svObj = new SalgsVare();
          try{ // the columns from the table SaleLineItem are used
        	 	svObj.setSalgsVare(new Ordre(null, 0, null, results.getInt("SalgsVareId"), null, null, null)); 
                svObj.setAntal(results.getInt("antal"));     
                svObj.setProduktId(new Produkt(0, results.getInt("produktId"), 0, null, null, null));
                
                
                
                }
         catch(Exception e)
         {
             System.out.println("error in building the saleLineItem object");
         }
         return svObj;
      }


	public SalgsVare findSalgsVareId(int svId, boolean retriveAssociation)
	{
		SalgsVare svObj = new SalgsVare();
		svObj = findWithPreQuery(svId, retriveAssociation);
		return svObj;
	}
	
	
	
	
	//finder 1 produkt via 1 id
    public SalgsVare findSalgsVare(int svId, boolean retriveAssociation)
    {   
    	String wClause = "  SalgsVareId = '" + svId + "'";
        return singleWhere(wClause, retriveAssociation);
    }

	public ArrayList<SalgsVare> visSalgsVare(int oId, boolean retriveAssociation) {
		
		String wClause = "  SalgsVareId = '" + oId + "'";
        return miscWhere(wClause, retriveAssociation);
	}
    
    
}